package com.zhongying.jf.util;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Created by Administrator on 2016/5/29 0029.
 */
public class WebUtil {

        private static final Logger logger = Logger.getLogger(WebUtil.class);

        public static String getIpAddr() {
            String ip = "";
            try {
                 ip = WebContext.getRequest().getHeader("X-Forwarded-For");
            }catch (Exception e) {
                return "127.0.0.1";
            }

            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = WebContext.getRequest().getRemoteAddr();
            } else {
                String[] candidates = ip.split(",");
                for (String cand : candidates) {
                    if (!("unknown".equalsIgnoreCase(cand))) {
                        ip = cand;
                        break;
                    }
                }
            }
            if(ip.equals("0:0:0:0:0:0:0:1")){
                ip = "127.0.0.1";
            }
            return ip;
        }

}
